package com.example.demo.shops.service;

import com.example.demo.security.bean.SecurityAdminRoleBean;
import com.example.demo.service.BaseService;
import com.example.demo.shops.bean.CarBean;
import com.example.demo.shops.bean.ShopBannerBean;
import com.example.demo.shops.bean.ShopBean;
import com.example.demo.shops.repository.ShopBannerRepository;
import com.example.demo.shops.repository.ShopRepository;
import com.example.demo.sicktype.bean.SickTypeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ShopService extends BaseService<ShopBean,ShopRepository> {

    @Autowired
    ShopBannerRepository shopBannerRepository;

    public void save(ShopBean entity, String[] bannerimg) {
        if (entity.getId()!=0){
             ShopBean bean = getById((int) entity.getId());
             bean.trans(entity);

             final List<ShopBannerBean> oldShopBanner = bean.getShopBannerBeans()==null?new ArrayList<>():bean.getShopBannerBeans();


             final List<ShopBannerBean> newShopBanner= entity.getShopBannerBeans()==null?new ArrayList<ShopBannerBean>():entity.getShopBannerBeans();


             List<ShopBannerBean> temp = null;
                 //求交集
                  temp = oldShopBanner.stream().filter(o -> newShopBanner.stream().anyMatch(n -> o.getImg().equals(n.getImg())
                 )).collect(Collectors.toList());

              //删除多余
            Set<ShopBannerBean> removeSet = new HashSet<>();
            removeSet = oldShopBanner.stream().filter(n->!newShopBanner.stream().anyMatch(o->
                            o.getImg().equals(n.getImg())
                    )
            ).collect(Collectors.toSet());



            shopBannerRepository.deleteInBatch(removeSet);

            //求差集
            List<ShopBannerBean> newShop = newShopBanner.stream().filter(n->
                    !oldShopBanner.stream().anyMatch(o->
                            o.getImg().equals(n.getImg())
                    )
            ).collect(Collectors.toList());

            temp.addAll(newShop);


            bean.setShopBannerBeans(temp);





        }else{
            entity.setShopBannerBeans(getShopBannerList(bannerimg));
        }
        save(entity);
    }


    private List<ShopBannerBean> getShopBannerList(String[] bannerimg){
       return Arrays.stream(bannerimg).map(s->{
            ShopBannerBean bannerBean = new ShopBannerBean();
            bannerBean.setImg(s);
            return bannerBean;
        }).collect(Collectors.toList());
    }


    public List<ShopBean> getShopsBySitckType(SickTypeBean bean){
        return repository.findAllBySickTypeBean(bean);
    }

    public List<CarBean> getcarlist(HashMap<Integer, Integer> hashMap) {

        if (hashMap==null)
        {
            return null;
        }
       return   hashMap.keySet().parallelStream().map(b->{
             ShopBean shopBean = getById(b);
             CarBean carBean = new CarBean();
             carBean.setId((int) shopBean.getId());
             carBean.setHeadimg(shopBean.getImg());
             carBean.setName(shopBean.getTitle());
             carBean.setPrice(String.valueOf(shopBean.getPrice()* hashMap.get(b)));
             carBean.setNumber(hashMap.get(b));
             return carBean;
         }).collect(Collectors.toList());

    }
}
